<?php
/**
 * 作用：统计分析
 * 作者: shijy@soe-soe.com
 * 日期: 2016/5/12 11:33
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */


namespace Admin\Model;
use Common\Model\PointMallModel;
use Think\Model;
class AnalysisModel extends Model{
	const SUCCESS   =0;//成功
	const FAILURE   =1;//失败
	/**
	 * 活动的内容类型统计分析
	 * Author: shijy
	 * @return array
	 */
	public static function getEventsAnalysis(){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$list=M("e_events",null,DB_MAIN_CFG)
			->group('contentType')
			->field("contentType,count(1) as totalCount,sum(applyCount) as totalApply,AVG(applyCount) as avgApply,sum(hot) as totalHot,AVG(hot) as avgHot")
			->select();

		$contentType=getContentTypeKv();
		foreach($list as $k=>$v){
			if(array_key_exists($v['contentType'],$contentType )){
				$list[$k]['contentType']=$contentType[$v['contentType']];
			}
		}
		$dataSuccess['data']=$list;
		return $dataSuccess;
	}

	/**
	 * 文章的内容类型统计分析
	 * Author: shijy
	 * @return array
	 */
	public static function getArticleAnalysis(){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$list=M("article",null,DB_MAIN_CFG)
			->group('contentType')
			->field("contentType ,count(1) as totalCount,sum(viewNum) as totalView,AVG(viewNum) as avgView,sum(commentNum) as totalComment,AVG(commentNum) as avgComment,sum(praiseNum) as totalPraise,AVG(praiseNum) as avgPraise")
			->select();

		$contentType=getContentTypeKv();
		foreach($list as $k=>$v){
			if(array_key_exists($v['contentType'],$contentType )){
				$list[$k]['contentType']=$contentType[$v['contentType']];
			}
		}
		$dataSuccess['data']=$list;
		return $dataSuccess;
	}

	/**
	 * 积分商城的内容类型统计分析
	 * Author: shijy
	 * @return array
	 */
	public static function getPointMallAnalysis(){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$list=M("point_mall",null,DB_MAIN_CFG)
			->group('contentType')
			->field("group_concat(id) as ids,contentType ,count(1) as totalCount,sum(buyNum) as totalTimes,AVG(buyNum) as avgTimes")
			->select();

		$count=M('exchange_log',null,DB_MAIN_CFG)->where('status='.PointMallModel::EXCHANGE_STATUS_SUCCESS)
			->group('pid')
			->field('pid, sum(costCash) as totalCash,sum(costPoint) as totalPoint')
			->select();

		$contentType=getContentTypeKv();
		foreach($list as $k=>$v){
			if(array_key_exists($v['contentType'],$contentType )){
				$list[$k]['contentType']=$contentType[$v['contentType']];
			}
			$v['totalCash']=0;
			$v['totalPoint']=0;


			foreach($count as $one){
				if (strpos( ','.$v['ids'].','  , ','.$one['pid'].',')===false){
					continue;
				}
				$v['totalCash'] +=$one['totalCash'];
				$v['totalPoint']+=$one['totalPoint'];
			}
			$list[$k]['totalCash']	=$v['totalCash'];
			$list[$k]['totalPoint']	=$v['totalPoint'];
			$list[$k]['avgCash']	=$v['totalCash']/$v['totalCount'];
			$list[$k]['avgPoint']	=$v['totalPoint']/$v['totalCount'];
		}
		$dataSuccess['data']=$list;
		return $dataSuccess;
	}


	/**
	 * 活跃统计数据
	 * Author: shijy
	 * @param $dailyOrMonthly
	 * @param $page
	 * @param $pageSize
	 * @return array
	 */
	public static function getActiveList($dailyOrMonthly,$page,$pageSize){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		switch (strtoupper($dailyOrMonthly)){
			case 'MONTHLY':
				$tableName='stat_active_monthly';
				break;
			case 'DAILY':
				$tableName='stat_active_daily';
				break;
			default:
				return $dataFailure;
				break;
		}


		$dataSuccess['total'] = M($tableName,null,DB_MAIN_CFG)->count();						//获取数据总条数
		$dataSuccess['page'] = $page ? $page :1;												//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);						//总页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >=$dataSuccess['totalPage']?0:1;		//是否还有下一页
		$offset = ($dataSuccess['page']-1) * $pageSize;

		$list = M($tableName,null,DB_MAIN_CFG)
			->limit($offset,$pageSize)
			->order("timeStr DESC")->select();
		$dataSuccess['data'] = $list;
		return $dataSuccess;
	}


}